package com.luinnc.zxingcodedemo;

import com.google.zxing.WriterException;
import com.zxing.activity.CaptureActivity;
import com.zxing.encoding.EncodingHandler;

import android.app.Activity;
import android.app.Instrumentation.ActivityResult;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.UrlQuerySanitizer.ParameterValuePair;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
	private Button scan;
	private Button getbitmap;
	private TextView scannerResult;
	private EditText genEdit;
	private ImageView viewbitmap;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		scannerResult = (TextView) findViewById(R.id.scannerResult);
		genEdit = (EditText) findViewById(R.id.genEdit);
		viewbitmap = (ImageView) findViewById(R.id.viewbitmap);
		scan = (Button) findViewById(R.id.scan);
		getbitmap = (Button) findViewById(R.id.genbitmap);

		scan.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent scanner = new Intent(MainActivity.this,
						CaptureActivity.class);
				startActivityForResult(scanner, 0);

			}
		});
		getbitmap.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String inString=genEdit.getText().toString();
				if (inString.equals("")) {
					Toast.makeText(MainActivity.this, "请输入要生成内容", Toast.LENGTH_LONG).show();
				}else {
					try {
						Bitmap bitmap=EncodingHandler.createQRCode(inString, 300);
						viewbitmap.setImageBitmap(bitmap);
					} catch (WriterException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
				
			}
		});
		

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK) {
			String codString = data.getExtras().getString("result");
			scannerResult.setText(codString);
		}
	}

}
